import os
from urllib import parse

import facade
from bs4 import BeautifulSoup
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curPath, -3)
sPath = BaseDir.get_new_path(TopPath, "download", "cnki_cg", "download", "Raw")

gClassName = 'OTHER'
ListSqls = list()
nStart = 1
nCount = 0


# 测试文字输出到文本
def output(value, files='log.txt'):
    f = open(files, 'w', encoding='utf-8')
    f.write(value)
    f.close()


mysqlutils = facade.MysqlUtiles(BaseDir.get_new_path(curPath, "db.ini"), "db", facade.get_streamlogger())


# # 数据库链接
# def MajorDbConnect():
#     return MySqlDbConnect(curPath, "db.ini")


# 获取wanId
def GetFileName(sPath):
    global ListSqls
    print('Start!!!')
    errcount = 0
    try:
        soup = BeautifulSoup(open(sPath, 'r', encoding='utf-8'), "lxml")

        table = soup.find('table', class_='GridTableContent')
        trs = table.find_all('tr')
        print('trs:' + str(len(trs)))
        for tr in trs[1:]:
            a = tr.find('a', class_='fz14')
            href = a.get('href')
            dbcode = parse.parse_qs(parse.urlparse(href).query)['dbcode'][0]
            dbname = parse.parse_qs(parse.urlparse(href).query)['dbname'][0]
            filename = parse.parse_qs(parse.urlparse(href).query)['filename'][0]
            sql = "INSERT IGNORE INTO article(filename, dbcode, dbname) VALUES('{filename}', '{dbcode}', '{dbname}') ".format(
                dbcode=dbcode, dbname=dbname, filename=filename)
            ListSqls.append(sql)

        InsertIntoDbFromList()
    except:
        print("出错")
        errcount += 1

    print("出错记数为" + str(errcount))



# 插入数据库
def InsertIntoDbFromList():
    global ListSqls
    # conn = MajorDbConnect()
    # ExeSqlList(ListSqls, conn)
    mysqlutils.ExeSqlListToDB(ListSqls)
    ListSqls = list()


# ----------------------------------------------逻辑部分-----------------------
# ----------------------------------------------录入数据库wanID-----------------
def DoWork():
    global sPath
    sPath = os.path.join(sPath, 'Raw')
    print('path:' + sPath)

    # fileCount =  len([j for i in [i[2] for i in os.walk(sPath)] for j in i])
    # print('filecount:%d' %fileCount)

    for path, dirNames, fileNames in os.walk(sPath):
        for fileName in fileNames:
            filePath = os.path.join(path, fileName)
            print(filePath)
            # AnalysisFile(filePath,fileName)
            GetFileName(filePath)

        # 修改工作存放html的工作目录
        # os.chdir(path)

        # 开始作业
        if len(ListSqls) > 0:
            InsertIntoDbFromList()

        print('DOWN TOTAL:' + str(nCount))


def main(logger=None):
    # sPath = os.path.join(sPath, 'Raw')
    # print('path:' + sPath)

    # fileCount =  len([j for i in [i[2] for i in os.walk(sPath)] for j in i])
    # print('filecount:%d' %fileCount)

    for path, dirNames, fileNames in os.walk(sPath):
        for fileName in fileNames:
            filePath = os.path.join(path, fileName)
            print(filePath)
            # AnalysisFile(filePath,fileName)
            GetFileName(filePath)

        # 修改工作存放html的工作目录
        # os.chdir(path)

        # 开始作业

        if len(ListSqls) > 0:
            InsertIntoDbFromList()

        print('DOWN TOTAL:' + str(nCount))


if __name__ == '__main__':
    main()
